Creating tables from scratch is a pain.
This is why there are many table plugins to let us add tables easily.
In this article, we’ll look at how to add tables to a Vue app with the vue-good-table plugin.
.vgt-table .striped
We can add row striping with the striped
class.
For example, we can write:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" styleClass="vgt-table striped"></vue-good-table>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Mary", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
<style>
.vgt-table {
border: 1px solid red !important;
}
</style>
Now the rows are striped.
.vgt-table .bordered
We can make the table row bordered with the bordered
class.
For example, we can write:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" styleClass="vgt-table bordered"></vue-good-table>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Mary", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
We add the bordered
class to add borders to rows and columns.
Condensed Table
To make the table condensed, we can add the condensed
class:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" styleClass="vgt-table condensed"></vue-good-table>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Mary", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
Sass
We can import SASS styling provided by the vue-good-table library by writing:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" styleClass="vgt-table condensed"></vue-good-table>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Mary", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
<style lang="scss">
@import "../node_modules/vue-good-table/src/styles/style.scss";
</style>
The @import
directive lets us import styles into our app.
Conclusion
We can add styles with various methods with the vue-good-table plugin.